<?php

namespace App\Http\Controllers\App;

use App\Http\Controllers\Controller;
use App\Models\Invoice;
use App\Models\Order;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;

class InvoiceController extends Controller
{
    private $user_id;
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {   
       $this->user_id = Auth::id();
    }
    //新增保存
    public function add(Request $request)
    {   
        $input = $request->input();
        $orderId = $input['orderId'];
        if (empty($input['orderId']) || empty($input['title']) || empty($input['taxesNo']) || empty($input['name']) || empty($input['mobile']) || empty($input['address']) ) {
            return $this->respond('empty_error');
        }

        $orderInfo = Order::where('id',$input['orderId'])->where('status',2)->first();
        if(empty($orderInfo))
        {
            $return = ['code'=>500,'msg'=>'不存在的订单id！'];
            return response()->json($return);
        }
        $result = Invoice::create([
            'userId'=>$this->user_id,
            'orderId'=>$input['orderId'],
            'title'=>$input['title'],
            'taxesNo'=>$input['taxesNo'],
            'name'=>$input['name'],
            'mobile'=>$input['mobile'],
            'address'=>$input['address'],
        ]);
        if ($result) {
            return $this->respond('add_ok',['orderInfo'=>$orderInfo,'invoiceInfo'=>$result]);
        }
        return $this->respond('add_error');
    }
}
